package com.example.leetcode;

/**
 * 动态规划
 * 不同路径2
 * f[i][j]=f[i-1][j]+f[i][j-1];
 */
public class Demo0063 {

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m=obstacleGrid.length;
        int n=obstacleGrid[0].length;
        int [][] f=new int[m][n];
        f[0][0]=1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if ((i-1)>=0 && obstacleGrid[i-1][j] ==0){
                    f[i][j]+=f[i-1][j];
                }
                if ((j-1)>=0 && obstacleGrid[i][j-1] ==0){
                    f[i][j]+=f[i][j-1];
                }
            }
        }
        return f[m-1][n-1];
    }

    public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
        int m=obstacleGrid.length;
        int n=obstacleGrid[0].length;
        int [][] f=new int[m][n];
        if (obstacleGrid[0][0]==1){
            return 0;
        }
        f[0][0]=1;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (obstacleGrid[i][j]==1){
                    continue;
                }
                f[i][j]+=((i-1)>=0?f[i-1][j]:0);
                f[i][j]+=((j-1)>=0?f[i][j-1]:0);
            }
        }
        return f[m-1][n-1];
    }
}
